package gitee

import (
	"bufio"
	"bytes"
	"os"
)

// PathDirExists todo
func PathDirExists(p string) bool {
	st, err := os.Stat(p)
	if err != nil {
		return false
	}
	return st.IsDir()
}

// PathExists path exists
func PathExists(p string) bool {
	if _, err := os.Stat(p); os.IsNotExist(err) {
		return false
	}
	return true
}

// ReadFileFirstLine read file first line
func ReadFileFirstLine(path string) (string, error) {
	file, err := os.Open(path)
	if err != nil {
		return "", err
	}
	defer file.Close()
	reader := bufio.NewReader(file)
	var part []byte
	var prefix bool
	buffer := bytes.NewBuffer(make([]byte, 0, 512))
	for {
		if part, prefix, err = reader.ReadLine(); err != nil {
			return "", err
		}
		if part == nil {
			return "", os.ErrInvalid
		}
		buffer.Write(part)
		if !prefix {
			return buffer.String(), nil
		}
	}
}
